<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>formdata解析</title>
</head>
<body>
    <h1>手动解析 formdata</h1>
    <div>

        选择文件(可多选):
            <input type="file" id="f1" multiple/><br/><br/>

            标题：<input type="text" id="title"/><br/><br/><br/>

        <button type="button" id="btn-submit">上 传</button>

        </form>
    </div>
</body>
</html>
<script>
    function submitUpload() {

        var fileList = document.getElementById('f1').files;
        if(!fileList.length){
            alert('请选择文件');
            return;
        }

        var fd = new FormData();   //构造FormData对象
        fd.append('title', document.getElementById('title').value);
        for(var i =0;i<fileList.length;i++){
            fd.append('f1', fileList[i]);//支持多文件上传
        }

        fetch('http://localhost:8100/', {
            method: 'POST',
            body: fd
        })
            .then(response => response.json())
            .then(response =>{
                console.log(response);
                if (response.fileUrl.length) {
                    //alert('上传成功');
                }
            } )
            .catch(error => console.error('Error:', error));
    }
    //绑定提交事件
    document.getElementById('btn-submit').addEventListener('click',submitUpload);

</script>